$(function(){

    $("body").bind("click",clearList);
    $(".listGroup>.btnPrimary").bind("click",clickListGroup);
    $(".listGroup>.dropDownList>li>a").bind("click",clickListItem);

    function clearList(){
        $(".listGroup").removeClass("open");
    }
    function clickListGroup(event){
        event.stopPropagation();
        var curParent = $(event.currentTarget).parent();
        curParent.toggleClass("open");
    }
    function clickListItem(event){
        event.stopPropagation();
        var cur = $(event.currentTarget);
        var curCurrent = cur.parent().parent().parent();
        if(cur.attr("link")!=undefined){
            location.href = cur.attr("link");
        }else if(cur.attr("value")!=undefined){
            $(curCurrent.find(".btnLeft")).html(cur.html());
            $(curCurrent.find(".btnLeft")).attr("value",cur.attr("value"));
            $(curCurrent.find("input")).val(cur.attr("value"));
            $(curCurrent.find("input")).change();
        }
        clearList();
    }
});